package com.ckx.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.ckx.domain.ResponseResult;
import com.ckx.domain.dto.CategoryAddDto;
import com.ckx.domain.dto.CategoryUpdateDto;
import com.ckx.domain.vo.CategoryUpdateVo;
import com.ckx.domain.entity.Category;
import com.ckx.domain.vo.CategoryVo;
import com.ckx.domain.vo.ExcelCategoryVo;
import com.ckx.domain.vo.PageVo;
import com.ckx.enums.AppHttpCodeEnum;
import com.ckx.service.CategoryService;
import com.ckx.utils.BeanCopyUtil;
import com.ckx.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory() {

        List<CategoryVo> categoryVos = categoryService.listAllCategory();
        return ResponseResult.okResult(categoryVos);

    }

    @PreAuthorize("@perm.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx", response);
            //获取需要导出的数据
            List<Category> categoryVos = categoryService.list();
            //封装vo
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtil.copyBeanList(categoryVos, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class)
                    .autoCloseStream(Boolean.FALSE)
                    .sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize, Category category){
        PageVo pageVo = categoryService.listByPage(pageNum, pageSize, category);
        return ResponseResult.okResult(pageVo);
    }

    @PostMapping
    public ResponseResult addCategory(@RequestBody CategoryAddDto categoryAddDto){
        categoryService.addCategory(categoryAddDto);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult getCategoryInfo(@PathVariable Long id){
        CategoryUpdateVo categoryUpdateVo = categoryService.getCategoryInfo(id);
        return ResponseResult.okResult(categoryUpdateVo);
    }

    @PutMapping
    public ResponseResult updateCategory(@RequestBody CategoryUpdateDto categoryUpdateDto){
        categoryService.updateCategory(categoryUpdateDto);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    public ResponseResult delCategory(@PathVariable Long id){
        categoryService.del4DelFlag(id);
        return ResponseResult.okResult();
    }

}
